package me.yricky.alia.ui.mainPage

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Outline
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.unit.*
import androidx.compose.ui.window.Window
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.yricky.alia.App
import me.yricky.alia.ui.AliaIcons
import me.yricky.alia.ui.WindowInst
import me.yricky.alia.ui.component.ARGBColorPicker
import me.yricky.alia.ui.fileListPage.FileListPageFactory
import me.yricky.alia.ui.themeColor

/**
 * @author Yricky
 * @date 2022/1/29
 */
object MainPageFactory {

    val drawerShape = object : Shape {
        override fun createOutline(
            size: Size,
            layoutDirection: LayoutDirection,
            density: Density
        ): Outline {
            return Outline.Rectangle(Rect(0f,0f,size.width.coerceAtMost(300 * density.density),size.height))
        }
    }

    fun getWindowInst():WindowInst{
        return object : WindowInst() {

            override fun tag(): String = "Alia"
            @Composable
            override fun window() {
                val drawerState by remember { mutableStateOf(DrawerState(DrawerValue.Closed)) }
                val scope = rememberCoroutineScope()
                var drawerContent:@Composable ColumnScope.()->Unit by remember { mutableStateOf(@Composable{Spacer(Modifier.weight(1f))}) }
                val mainColor by themeColor.collectAsState()
                Window(
                    title = "Alia",
                    icon = AliaIcons.defaultWindowIcon(),
                    onCloseRequest = { App.removeWindow(this) }) {
                    window.isResizable = false
                    Scaffold(
                        topBar = {
                            Icon(Icons.Rounded.Menu,"",Modifier.clickable {
                                scope.launch { drawerState.open()}
                            }.size(48.dp).padding(6.dp))
                                 },
                        drawerContent = { Column(Modifier.width(240.dp)) {
                            drawerContent(this)
                            Spacer(Modifier.weight(1f))
                            Icon(Icons.Rounded.ColorLens,"",Modifier.clickable {
                                drawerContent = {
                                    Column {
                                        Icon(
                                            Icons.Rounded.ArrowBack,"",
                                            Modifier.clickable { drawerContent = {  } }.size(48.dp).padding(6.dp)
                                        )
                                        ARGBColorPicker(modifier = Modifier.fillMaxWidth(), initColor = themeColor.value){
                                            themeColor.value = it
                                        }
                                    }
                                }
                            }.size(48.dp).padding(6.dp),
                                tint = mainColor)
                        } },
                        scaffoldState = rememberScaffoldState(drawerState),
                        drawerShape = drawerShape
                    ) { Page() }

                }
            }
        }
    }

    @OptIn(ExperimentalFoundationApi::class)
    @Composable
    fun Page(){
        val res by App.res.collectAsState()
        Column (
            Modifier
                .fillMaxSize(),
            horizontalAlignment = Alignment.CenterHorizontally
        ){
            Spacer(Modifier.weight(1f))
            Text("Alia!!", fontSize = 48.sp)
            Text("Alia is a layout tool. Alia is made by Yricky.")
            Row {
                Column(
                    Modifier
                        .clickable { App.addWindow(FileListPageFactory.getWindowInst(".")) }
                        .padding(24.dp),
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    Icon(Icons.Rounded.OpenInNew,"",Modifier
                        .size(96.dp)
                        .padding(6.dp)
                    )
                    Text(res.str.open, fontSize = 24.sp)
                }
                Column(
                    Modifier
                        .clickable {  }
                        .padding(24.dp),
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    Icon(Icons.Rounded.Star,"",Modifier.size(96.dp))
                    Text(res.str.arcane, fontSize = 24.sp)
                }
            }
            Spacer(Modifier.weight(1.2f))
        }
    }
}